load("//bazel:ray.bzl", "define_java_module")
load("//bazel:ray.bzl", "native_java_binary")
load("//bazel:ray.bzl", "native_java_library")
load("@rules_proto_grpc//java:defs.bzl", "java_proto_compile")

exports_files([
    "testng.xml",
])

all_modules = [
    "streaming-state",
    "streaming-api",
    "streaming-runtime",
]

java_import(
    name = "all_modules",
    jars = [
        "libio_ray_ray_" + module + ".jar"
        for module in all_modules
    ] + [
        "libio_ray_ray_" + module + "-src.jar"
        for module in all_modules
    ] + [
        "all_streaming_tests_deploy.jar",
        "all_streaming_tests_deploy-src.jar",
    ],
    deps = [
        ":io_ray_ray_" + module
        for module in all_modules
    ] + [
        ":all_streaming_tests",
    ],
)

define_java_module(
    name = "streaming-api",
    define_test_lib = True,
    test_deps = [
        "//java:io_ray_ray_api",
        ":io_ray_ray_streaming-state",
        ":io_ray_ray_streaming-api",
        "@ray_streaming_maven//:com_google_guava_guava",
        "@ray_streaming_maven//:org_apache_commons_commons_lang3",
        "@ray_streaming_maven//:org_slf4j_slf4j_api",
        "@ray_streaming_maven//:org_slf4j_slf4j_log4j12",
        "@ray_streaming_maven//:org_testng_testng",
    ],
    visibility = ["//visibility:public"],
    deps = [
        ":io_ray_ray_streaming-state",
        "//java:io_ray_ray_api",
        "//java:io_ray_ray_runtime",
        "@ray_streaming_maven//:com_google_code_findbugs_jsr305",
        "@ray_streaming_maven//:com_google_code_gson_gson",
        "@ray_streaming_maven//:com_google_guava_guava",
        "@ray_streaming_maven//:org_apache_commons_commons_lang3",
        "@ray_streaming_maven//:org_slf4j_slf4j_api",
        "@ray_streaming_maven//:org_slf4j_slf4j_log4j12",
    ],
)

define_java_module(
    name = "streaming-state",
    define_test_lib = True,
    test_deps = [
        ":io_ray_ray_streaming-state",
        "@ray_streaming_maven//:com_google_guava_guava",
        "@ray_streaming_maven//:org_slf4j_slf4j_api",
        "@ray_streaming_maven//:org_slf4j_slf4j_log4j12",
        "@ray_streaming_maven//:org_testng_testng",
        "@ray_streaming_maven//:org_mockito_mockito_all",
        "@ray_streaming_maven//:de_ruedigermoeller_fst",
        "@ray_streaming_maven//:org_apache_commons_commons_lang3",
    ],
    visibility = ["//visibility:public"],
    deps = [
        "@ray_streaming_maven//:com_google_guava_guava",
        "@ray_streaming_maven//:de_ruedigermoeller_fst",
        "@ray_streaming_maven//:log4j_log4j",
        "@ray_streaming_maven//:org_apache_commons_commons_lang3",
        "@ray_streaming_maven//:org_slf4j_slf4j_api",
        "@ray_streaming_maven//:org_slf4j_slf4j_log4j12",
    ],
)

native_java_library("streaming-runtime", "streaming_java", "//streaming:libstreaming_java.so")

filegroup(
    name = "java_native_deps",
    srcs = [":streaming_java"],
)

define_java_module(
    name = "streaming-runtime",
    additional_resources = [
        ":java_native_deps",
    ],
    additional_srcs = [
        ":all_java_proto",
    ],
    define_test_lib = True,
    exclude_srcs = [
        "streaming-runtime/src/main/java/io/ray/streaming/runtime/generated/*.java",
    ],
    test_deps = [
        "//java:io_ray_ray_api",
        "//java:io_ray_ray_runtime",
        ":io_ray_ray_streaming-state",
        ":io_ray_ray_streaming-api",
        ":io_ray_ray_streaming-runtime",
        "@ray_streaming_maven//:com_google_guava_guava",
        "@ray_streaming_maven//:com_google_code_findbugs_jsr305",
        "@ray_streaming_maven//:org_apache_commons_commons_lang3",
        "@ray_streaming_maven//:de_ruedigermoeller_fst",
        "@ray_streaming_maven//:org_aeonbits_owner_owner",
        "@ray_streaming_maven//:org_slf4j_slf4j_api",
        "@ray_streaming_maven//:org_slf4j_slf4j_log4j12",
        "@ray_streaming_maven//:org_testng_testng",
        "@ray_streaming_maven//:org_mockito_mockito_all",
        "@ray_streaming_maven//:org_powermock_powermock_api_mockito",
        "@ray_streaming_maven//:org_powermock_powermock_module_testng",
    ],
    visibility = ["//visibility:public"],
    deps = [
        ":io_ray_ray_streaming-api",
        ":io_ray_ray_streaming-state",
        "//java:io_ray_ray_api",
        "//java:io_ray_ray_runtime",
        "@maven//:commons_io_commons_io",
        "@ray_streaming_maven//:com_github_davidmoten_flatbuffers_java",
        "@ray_streaming_maven//:com_google_code_findbugs_jsr305",
        "@ray_streaming_maven//:com_google_guava_guava",
        "@ray_streaming_maven//:com_google_protobuf_protobuf_java",
        "@ray_streaming_maven//:commons_collections_commons_collections",
        "@ray_streaming_maven//:de_ruedigermoeller_fst",
        "@ray_streaming_maven//:org_aeonbits_owner_owner",
        "@ray_streaming_maven//:org_apache_commons_commons_lang3",
        "@ray_streaming_maven//:org_msgpack_msgpack_core",
        "@ray_streaming_maven//:org_slf4j_slf4j_api",
        "@ray_streaming_maven//:org_slf4j_slf4j_log4j12",
    ],
)

java_binary(
    name = "all_streaming_tests",
    args = ["streaming/java/testng.xml"],
    data = ["testng.xml"],
    main_class = "org.testng.TestNG",
    runtime_deps = [
        ":io_ray_ray_streaming-api_test",
        ":io_ray_ray_streaming-runtime",
        ":io_ray_ray_streaming-runtime_test",
        ":io_ray_ray_streaming-state",
        "//java:io_ray_ray_runtime",
        "@ray_streaming_maven//:org_mockito_mockito_all",
        "@ray_streaming_maven//:org_powermock_powermock_api_mockito",
        "@ray_streaming_maven//:org_powermock_powermock_module_testng",
        "@ray_streaming_maven//:org_testng_testng",
    ],
)

# proto buffer
java_proto_compile(
    name = "streaming_java_proto",
    deps = ["//streaming:streaming_proto"],
)

java_proto_compile(
    name = "remote_call_java_proto",
    deps = ["//streaming:remote_call_proto"],
)

filegroup(
    name = "all_java_proto",
    srcs = [
        ":remote_call_java_proto",
        ":streaming_java_proto",
    ],
)

genrule(
    name = "copy_pom_file",
    srcs = [
        "//streaming/java:io_ray_ray_" + module + "_pom"
        for module in all_modules
    ],
    outs = ["copy_pom_file.out"],
    cmd = """
        WORK_DIR="$$(pwd)"
        cp -f $(location //streaming/java:io_ray_ray_streaming-api_pom) "$$WORK_DIR/streaming/java/streaming-api/pom.xml"
        cp -f $(location //streaming/java:io_ray_ray_streaming-runtime_pom) "$$WORK_DIR/streaming/java/streaming-runtime/pom.xml"
        date > $@
    """,
    local = 1,
    tags = ["no-cache"],
)

genrule(
    name = "cp_java_generated",
    srcs = [
        ":all_java_proto",
        ":copy_pom_file",
    ],
    outs = ["cp_java_generated.out"],
    cmd = """
        WORK_DIR="$$(pwd)"
        GENERATED_DIR="$$WORK_DIR/streaming/java/streaming-runtime/src/main/java/io/ray/streaming/runtime/generated"
        rm -rf "$$GENERATED_DIR"
        mkdir -p "$$GENERATED_DIR"
        # Copy protobuf-generated files.
        for f in $(locations //streaming/java:all_java_proto); do
            unzip -q -o "$$f" -x META-INF/MANIFEST.MF -d "$$WORK_DIR/streaming/java/streaming-runtime/src/main/java"
        done
        # remove third party protobuf
        rm -rf $$WORK_DIR/streaming/java/streaming-runtime/src/main/java/com/google/protobuf/
        date > $@
    """,
    local = 1,
    tags = ["no-cache"],
)

# Generates the dependencies needed by maven.
genrule(
    name = "gen_maven_deps",
    srcs = [
        ":java_native_deps",
        ":cp_java_generated",
    ],
    outs = ["gen_maven_deps.out"],
    cmd = """
        WORK_DIR="$${PWD}"
        # Copy native dependencies.
        OS_NAME=""
        case "$${OSTYPE}" in
          linux*) OS_NAME="linux";;
          darwin*) OS_NAME="darwin";;
          *) echo "$${OSTYPE} is not supported currently"; exit 1;;
        esac
        NATIVE_DEPS_DIR="$$WORK_DIR/streaming/java/streaming-runtime/native_dependencies/native/$$OS_NAME"
        rm -rf "$$NATIVE_DEPS_DIR"
        mkdir -p "$$NATIVE_DEPS_DIR"
        for f in $(locations //streaming/java:java_native_deps); do
            chmod +w "$$f"
            cp "$$f" "$$NATIVE_DEPS_DIR"
        done
        date > $@
    """,
    local = 1,
    tags = ["no-cache"],
    visibility = ["//visibility:public"],
)
